package com.bitdf.txing.chat.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bitdf.txing.chat.dao.FriendRelationDao;
import com.bitdf.txing.chat.entity.FriendRelationEntity;
import com.bitdf.txing.chat.feign.ChatUserFeignService;
import com.bitdf.txing.chat.service.FriendRelationService;
import com.bitdf.txing.common.utils.PageUtils;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.common.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("friendRelationService")
public class FriendRelationServiceImpl extends ServiceImpl<FriendRelationDao, FriendRelationEntity> implements FriendRelationService {

    @Autowired
    ChatUserFeignService txUserFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<FriendRelationEntity> page = this.page(
//                new Query<FriendRelationEntity>().getPage(params),
//                new QueryWrapper<FriendRelationEntity>()
//        );
//
//        return new PageUtils(page);
        return null;
    }

    /**
     * 获取当前登录用户的好友
     * @param l
     * @return
     */
    @Override
    public List<UserInfoVo> findFriends(long l) {
        List<FriendRelationEntity> list = this.list(new QueryWrapper<FriendRelationEntity>().eq("user_id", l).or().eq("friend_id", l));
        if(list==null || list.size()==0){
            return null;
        }
        List<Long> collect = list.stream().map((item) -> {
            return item.getFriendId();
        }).collect(Collectors.toList());
        //根据好友id集合调用远程服务查询各用户对应的详细信息
        R r = txUserFeignService.getByUserList(collect);
        List<UserInfoVo> data = r.getData(new TypeReference<List<UserInfoVo>>() {
        });
        return data;
    }

    @Override
    public List<Long> findFriendIds(Long selfId) {
        List<FriendRelationEntity> list = this.list(new QueryWrapper<FriendRelationEntity>().eq("user_id", selfId).or().eq("friend_id", selfId));
        List<Long> collect = list.stream().map((entity) -> {
            return entity.getUserId() .equals( selfId) ? entity.getFriendId() : entity.getUserId();
        }).collect(Collectors.toList());
        return collect;
    }


}